"""
Copyright (c) 2023 - present SKbioinfo
"""

import os,pymysql
from apps.config import Config
from apps.NGS_API.utils.log_helper import lg
from functools import wraps

DB_ENGINE   = os.getenv('DB_ENGINE'   , Config.DB_ENGINE)
DB_USERNAME = os.getenv('DB_USERNAME' , Config.DB_USERNAME)
DB_PASS     = os.getenv('DB_PASS'     , Config.DB_PASS)
DB_HOST     = os.getenv('DB_HOST'     , Config.DB_HOST)
DB_PORT     = os.getenv('DB_PORT'     , Config.DB_PORT)
DB_NAME     = os.getenv('DB_NAME'     , Config.DB_NAME)

class MysqlConnection(object):
    """
        Mysql class: Add, Delete, Put, Query for database
    """
    def __init__(self):
        ### Connect to the database ###
        self.connection = pymysql.connect( 
                host=DB_HOST,
		        user=DB_USERNAME,
                password=DB_PASS,
		        db=DB_NAME
        )
        self.cursor = self.connection.cursor()
        self.cursor.execute("SET FOREIGN_KEY_CHECKS=0;")
        self.d_cursor = self.connection.cursor(pymysql.cursors.DictCursor)
        self.d_cursor.execute("SET FOREIGN_KEY_CHECKS=0;")
        
    def sudden_connect(operation): 
        def outer(func):
            @wraps(func)
            def wrapper(*args,**kwargs):
                ### set up MYSQL connection ###
                db_conn=MysqlConnection()
                ### execute cmd ###
                sql = func(*args,**kwargs)
                if operation == "Delete":
                    db_conn.Delete(sql)
                elif operation == "Add":
                    db_conn.Add(sql)
                elif operation == "Put":
                    db_conn.Put(sql)
                elif operation == "Query":
                    res = db_conn.Query(sql)
                    return res
                elif operation == "dic_Query":
                    res = db_conn.dic_Query(sql)
                    return res 
                ### close MSYQL connection ###
                db_conn.close_conn()
            return wrapper
        return outer
    ### register sudden_connect to mysql connection ###
    sudden_connect=staticmethod(sudden_connect)

    def Query(self, sql):
        """
        查询数据
        :param sql:
        :return:
        """
        self.cursor.execute(sql)
        return self.cursor.fetchall()
    
    def dic_Query(self,sql):
        """
        查询数据
        :param sql:
        :return:
        """
        self.d_cursor.execute(sql)
        return self.d_cursor.fetchall()
    
    def Add(self, sql):
        """
        增加数据
        :param sql:
        :return:
        """
        try:
            self.cursor.execute(sql)
            self.connection.commit()
        except Exception as e:
            lg.error(e)
            self.connection.rollback()
    
    def Delete(self,sql):
        """
        删除数据
        :param sql:
        :return:
        """
        try:
            self.cursor.execute(sql)
            self.connection.commit()
        except Exception as e:
            lg.error(e)
            self.connection.rollback()
    
    def Put(self, sql):
        """
        更改数据
        :param sql:
        :return:
        """
        try:
            self.cursor.execute(sql)
            self.connection.commit()
        except Exception as e:
            lg.error(e)
            self.connection.rollback()
        
    def close_conn(self):
        self.connection.close()